<?php
?>
<script type="text/javascript">
function changeAttribute(attr) {
	<?php
	$string = "";
	$countCA = 0;
	foreach ($_GET as $getName => $getValue) {
		if ($getName != "selectedA") { // if there is a selectedA get variable, then don't include it in the string, since we will be using a different selectedA value, based on the choice in the dropdown box
			if ($countCA == 0) { // if first time writing something to string, then don't make '&' the first character
				$string = $string . $getName . "=" . $getValue;
			}
			else {
				$string = $string . "&" . $getName . "=" . $getValue;
			}
			$countCA += 1;
		}
	}
	$string = str_ireplace('/', '%2F', $string);
	$string = str_ireplace(' ', '+', $string);
	$string = str_ireplace(';', '%3B', $string);
	$string = "\"".$string."\"";
	?>
	var string = <?php print $string; ?>;
	location.href="cropDatabaseInfo.php?"+string+"&selectedA="+attr;
}
</script>
<?php
// multiple crops summed yearly over multiple years. Multiple attributes.
$startYear = intval($startYear);
$endYear = intval($endYear);

//this simply creates the headers and creates an array containing the exact names of the selected attributes
$arrayOfSelectedAttributes = array();
if ($_GET['amount_harvested'] == 'on') {
	$arrayOfSelectedAttributes[] = 'amount_harvested';
}
if ($_GET['amount_donated'] == 'on') {
	$arrayOfSelectedAttributes[] = 'amount_donated';
}
if ($_GET['sales'] == 'on') {
	$arrayOfSelectedAttributes[] = 'sales';
}
if ($_GET['number_planted'] == 'on') {
	$arrayOfSelectedAttributes[] = 'number_planted';
}
if ($_GET['seeds_bought'] == 'on') {
	$arrayOfSelectedAttributes[] = 'seeds_bought';
}
if ($_GET['pest_issues'] == 'on') {
	$arrayOfSelectedAttributes[] = 'pest_issues';
}
if ($_GET['weed_issues'] == 'on') {
	$arrayOfSelectedAttributes[] = 'weed_issues';
}

$allCrops = $_GET['crops'];
$arrayOfCrops = explode(';', $allCrops);
foreach ($arrayOfCrops as $c) {
	$result = $result . "<th>" . $c . "</th>";
}

print "<form action=\"\">";
print "<select id=\"selectForm\" onchange=\"changeAttribute(this.value)\" name=\"attributes\">";
foreach ($arrayOfSelectedAttributes as $a) {
	if (isset($_GET['selectedA']) && $_GET['selectedA'] == $a) {
		print "<option selected=\"selected\" value=\"" . $a . "\">" . attributeFormat($a) . "</option>";
	}
	else {
		print "<option value=\"" . $a . "\">" . attributeFormat($a) . "</option>";
	}
}
print "</select>";
print "</form>";


$result = $result . "</tr></thead><tbody>";

$flag = 0;
$cropName = $_GET['crops'];

if (isset($_GET['selectedA'])) {
	$selectedA = $_GET['selectedA'];
}
else {
	$selectedA = $arrayOfSelectedAttributes[0];
}

for($i = $startYear; $i <= $endYear; $i++){
	$result = $result . "<tr><td><img id=\"$i\" src=\"DataTables-1.7.3/details_open.png\" /></td><td>$i</td>";
	$endOfStartYear = $startYear . "-12-31";
	$cropTracker = 0;
	for ($j = 0; $j < count($arrayOfCrops); $j++) {
		if ($flag < count($arrayOfCrops)){ //for the first row, to have the right start date
			$cropRow = getMultiYearCropData($startDate, $endOfStartYear, getCropId(substr($arrayOfCrops[$j], 0, strripos($arrayOfCrops[$j], " - "))), $selectedA, getUnitId($units[$cropTracker]), $selectedA == 'sales' ? getCustomerId($_GET['customers']) : ($selectedA == 'amount_donated' ? getCustomerId($_GET['donationCustomers']) : NULL)); //the thing inside getCropId simply cuts the string that contains the crop name and a ' - ' and the unit name, and what remains is just the cropname
			$flag += 1;
		}
		elseif (intval($i) == intval($endYear)) { //for the last one, to have the right end date
			$cropRow = getMultiYearCropData($i."-01-01", $endDate, getCropId(substr($arrayOfCrops[$j], 0, strripos($arrayOfCrops[$j], " - "))), $selectedA, getUnitId($units[$cropTracker]), $selectedA == 'sales' ? getCustomerId($_GET['customers']) : ($selectedA == 'amount_donated' ? getCustomerId($_GET['donationCustomers']) : NULL));
		}
		else { //for all the in-between years, that aren't start or end years
			$cropRow = getMultiYearCropData($i."-01-01", $i."-12-31", getCropId(substr($arrayOfCrops[$j], 0, strripos($arrayOfCrops[$j], " - "))), $selectedA, getUnitId($units[$cropTracker]), $selectedA == 'sales' ? getCustomerId($_GET['customers']) : ($selectedA == 'amount_donated' ? getCustomerId($_GET['donationCustomers']) : NULL));
			
		}
		foreach ($cropRow as $year => $result1) { //print out the data into the cell
			$result = $result . "<td>" . $result1 . "</td>";
		}
		if (count($cropRow) == 0) { //if there is no data, print an empty cell
			$result = $result . "<td></td>";
		}
		$cropTracker += 1;
	}	
	$result = $result . "</tr>";
}
?>